package com.naiterui.ehp.bs.cms.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.cms.common.utils.DateUtils;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.oms.entity.RefundOrder;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.RefundOrderLogMapper;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.RefundOrderMapper;
import com.naiterui.ehp.bs.cms.modules.oms.service.IRefundOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundOrderLogVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundOrderVO;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class RefundOrderServiceImpl extends ServiceImpl<RefundOrderMapper, RefundOrder> implements IRefundOrderService {

  private final RefundOrderLogMapper refundOrderLogMapper;

  @Override
  public PageVO<RefundOrderVO> queryPage(PageParamsVO pageParamsVO, RefundOrderVO refundOrderVO) {
    QueryWrapper<RefundOrderVO> entityWrapper = this.buildEntityWrapper(pageParamsVO, refundOrderVO);
    Page<RefundOrderVO> page = this.baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);
    return PageVO.toPageVo(page);
  }

  @Override
  public RefundOrderDetailVO getRefundOrderDetailById(Long refundOrderId) {
    RefundOrderDetailVO orderDetailVO = this.baseMapper.getRefundOrderDetailById(refundOrderId);
    List<RefundOrderLogVO> refundOrderLogs = this.refundOrderLogMapper.findByRefundOrderId(refundOrderId);
    orderDetailVO.setLogs(refundOrderLogs);
    return orderDetailVO;
  }

  private QueryWrapper<RefundOrderVO> buildEntityWrapper(PageParamsVO pageParamsVO, RefundOrderVO refundOrderVO) {
    QueryWrapper<RefundOrderVO> entityWrapper = pageParamsVO.getEntityWrapper("t");

    entityWrapper.eq("t.del_flag", 0);

    // 订单号
    String orderSn = refundOrderVO.getOrderSn();
    entityWrapper.eq(StringUtils.isNotBlank(orderSn), "t.order_sn", orderSn);

    //退款单号
    String refundOrderSn = refundOrderVO.getRefundOrderSn();
    entityWrapper.eq(StringUtils.isNotBlank(refundOrderSn), "t.refund_order_sn", refundOrderSn);

    //单据状态
    Integer status = refundOrderVO.getStatus();
    entityWrapper.eq(status != null, "t.status", status);

    //退款方式
    Integer method = refundOrderVO.getMethod();
    entityWrapper.eq(method != null, "t.method", method);

    // 审核时间
    String auditDateGte = refundOrderVO.getAuditDateGte();
    entityWrapper.ge(StringUtils.isNotBlank(auditDateGte), "t.audit_time", auditDateGte);

    String auditDateLte = refundOrderVO.getAuditDateLte();
    if (StringUtils.isNotBlank(auditDateLte)) {
      if (DateUtils.isNow(auditDateLte)) {
        Date plus = DateUtils.plus(DateUtils.parseStrToDate(auditDateLte), 1, ChronoUnit.DAYS);
        entityWrapper.le("t.audit_time", plus);
      } else {
        entityWrapper.le("t.audit_time", auditDateLte);
      }
    }
    return entityWrapper;
  }

}
